package com.koushikdutta.ion.loader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.text.TextUtils;
import com.koushikdutta.async.DataEmitter;
import com.koushikdutta.async.FileDataEmitter;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.async.future.SimpleFuture;
import com.koushikdutta.async.http.AsyncHttpRequest;
import com.koushikdutta.async.util.StreamUtility;
import com.koushikdutta.ion.Ion;
import com.koushikdutta.ion.Loader.LoaderEmitter;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.bitmap.IonBitmapCache;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;

public class FileLoader extends StreamLoader {

    private static final class FileFuture extends SimpleFuture<DataEmitter> {
        private FileFuture() {
        }
    }

    public Future<BitmapInfo> loadBitmap(Context context, Ion ion, String key, String uri, int resizeWidth, int resizeHeight, boolean animateGif) {
        if (uri == null || !uri.startsWith("file:/")) {
            return null;
        }
        final Future<BitmapInfo> ret = new SimpleFuture();
        final String str = uri;
        final Ion ion2 = ion;
        final int i = resizeWidth;
        final int i2 = resizeHeight;
        final boolean z = animateGif;
        final String str2 = key;
        Ion.getBitmapLoadExecutorService().execute(new Runnable() {
            public void run() {
                if (!ret.isCancelled()) {
                    try {
                        Object info;
                        File file = new File(URI.create(str));
                        Options options = ion2.getBitmapCache().prepareBitmapOptions(file, i, i2);
                        Point size = new Point(options.outWidth, options.outHeight);
                        if (z && TextUtils.equals("image/gif", options.outMimeType)) {
                            info = FileLoader.this.loadGif(str2, size, new FileInputStream(file), options);
                            StreamUtility.closeQuietly(new FileInputStream(file));
                        } else {
                            if (IonBitmapCache.loadBitmap(file, options) == null) {
                                throw new Exception("Bitmap failed to load");
                            }
                            info = new BitmapInfo(str2, options.outMimeType, new Bitmap[]{bitmap}, size);
                        }
                        info.loadedFrom = 1;
                        ret.setComplete(info);
                    } catch (OutOfMemoryError e) {
                        ret.setComplete(new Exception(e), null);
                    } catch (Exception e2) {
                        ret.setComplete(e2);
                    } catch (Throwable th) {
                        StreamUtility.closeQuietly(fin);
                    }
                }
            }
        });
        return ret;
    }

    public Future<DataEmitter> load(Ion ion, AsyncHttpRequest request, FutureCallback<LoaderEmitter> callback) {
        if (!request.getUri().getScheme().startsWith("file")) {
            return null;
        }
        final Future<DataEmitter> ret = new FileFuture();
        final AsyncHttpRequest asyncHttpRequest = request;
        final Ion ion2 = ion;
        final FutureCallback<LoaderEmitter> futureCallback = callback;
        ion.getHttpClient().getServer().post(new Runnable() {
            public void run() {
                File file = new File(URI.create(asyncHttpRequest.getUri().toString()));
                FileDataEmitter emitter = new FileDataEmitter(ion2.getHttpClient().getServer(), file);
                ret.setComplete((Object) emitter);
                futureCallback.onCompleted(null, new LoaderEmitter(emitter, (long) ((int) file.length()), 1, null, asyncHttpRequest));
            }
        });
        return ret;
    }
}
